#include <opencv2/opencv.hpp>
#include <iostream>
#include "streamPushWrapper.h"

// 辅助函数：详细分析Mat格式
void analyzeMatFormat(const cv::Mat& mat) {
    std::cout << "\n=== 详细图像格式分析 ===" << std::endl;
    
    // 基本信息
    std::cout << "尺寸: " << mat.cols << " x " << mat.rows << std::endl;
    std::cout << "通道数: " << mat.channels() << std::endl;
    std::cout << "总像素数: " << mat.total() << std::endl;
    
    // 数据类型分析
    int depth = mat.depth();
    std::string depth_str;
    switch (depth) {
        case CV_8U:  depth_str = "CV_8U (8-bit unsigned)"; break;
        case CV_8S:  depth_str = "CV_8S (8-bit signed)"; break;
        case CV_16U: depth_str = "CV_16U (16-bit unsigned)"; break;
        case CV_16S: depth_str = "CV_16S (16-bit signed)"; break;
        case CV_32S: depth_str = "CV_32S (32-bit signed)"; break;
        case CV_32F: depth_str = "CV_32F (32-bit float)"; break;
        case CV_64F: depth_str = "CV_64F (64-bit double)"; break;
        default: depth_str = "Unknown"; break;
    }
    std::cout << "数据深度: " << depth_str << std::endl;
    
    // 完整类型信息
    int type = mat.type();
    std::cout << "OpenCV类型码: " << type << std::endl;
    
    // 常见格式判断
    std::string format_description;
    if (type == CV_8UC1) {
        format_description = "8位灰度图像 (每像素1字节)";
    } else if (type == CV_8UC3) {
        format_description = "8位BGR彩色图像 (每像素3字节，蓝-绿-红顺序)";
    } else if (type == CV_8UC4) {
        format_description = "8位BGRA图像 (每像素4字节，含透明通道)";
    } else if (type == CV_16UC1) {
        format_description = "16位灰度图像 (每像素2字节)";
    } else if (type == CV_16UC3) {
        format_description = "16位BGR图像 (每像素6字节)";
    } else if (type == CV_32FC1) {
        format_description = "32位浮点灰度图像 (每像素4字节)";
    } else if (type == CV_32FC3) {
        format_description = "32位浮点BGR图像 (每像素12字节)";
    } else {
        format_description = "自定义或罕见格式";
    }
    
    std::cout << "格式描述: " << format_description << std::endl;
    
    // 内存布局信息
    std::cout << "每行字节数(step): " << mat.step << std::endl;
    std::cout << "每个元素字节数: " << mat.elemSize() << std::endl;
    std::cout << "每个通道字节数: " << mat.elemSize1() << std::endl;
    std::cout << "总内存占用: " << mat.total() * mat.elemSize() << " 字节" << std::endl;
    
    // 是否连续存储
    std::cout << "内存连续性: " << (mat.isContinuous() ? "连续" : "非连续") << std::endl;
    
    // 像素值范围信息
    if (mat.channels() >= 3 && mat.rows > 0 && mat.cols > 0) {
        // 获取几个样本像素值
        cv::Vec3b center_pixel = mat.at<cv::Vec3b>(mat.rows/2, mat.cols/2);
        cv::Vec3b corner_pixel = mat.at<cv::Vec3b>(0, 0);
        
        std::cout << "\n像素值示例:" << std::endl;
        std::cout << "中心像素 (BGR): B=" << (int)center_pixel[0] 
                  << ", G=" << (int)center_pixel[1] 
                  << ", R=" << (int)center_pixel[2] << std::endl;
        std::cout << "左上角像素 (BGR): B=" << (int)corner_pixel[0] 
                  << ", G=" << (int)corner_pixel[1] 
                  << ", R=" << (int)corner_pixel[2] << std::endl;
    }
    
    std::cout << "========================" << std::endl;
}


int main() {
    
    cv::VideoCapture cap(0);
    
    if (!cap.isOpened()) {
        std::cerr << "错误：无法打开摄像头！" << std::endl;
        return -1;
    }
    
    cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);   
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);  
    cap.set(cv::CAP_PROP_FPS, 30);            
    
    double fps = cap.get(cv::CAP_PROP_FPS);
    int width = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
    int height = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
    
    std::cout << "摄像头信息：" << std::endl;
    std::cout << "分辨率: " << width << "x" << height << std::endl;
    std::cout << "帧率: " << fps << " FPS" << std::endl;
    
    cv::namedWindow("摄像头画面", cv::WINDOW_AUTOSIZE);
    
    cv::Mat frame;
    bool first_frame = true;
    
    std::cout << "按 'q' 或 'ESC' 键退出..." << std::endl;

    //推流相关
    {
        createPushService();
        auto pUrl = getUrlFromPushService();

        printf("url:%s.version:%s\n",getUrlFromPushService(),getPushServiceVersion());
        

        setVideoInfoForPushService(width,height,fps,0.5);
        printf("setVideoInfoForPushService over!..\n");
    }
    
    while (true) {
        bool ret = cap.read(frame);
        
        if (!ret) {
            std::cerr << "错误：无法读取摄像头数据！" << std::endl;
            break;
        }
        
        if (first_frame) {
            analyzeMatFormat(frame);
            std::cout << "\n按 'c' 键查看颜色转换示例..." << std::endl;
            first_frame = false;
        }
        
        cv::putText(frame, "Press 'q' or 'ESC' to quit", 
                   cv::Point(10, 30), 
                   cv::FONT_HERSHEY_SIMPLEX, 
                   0.7, 
                   cv::Scalar(0, 255, 0), 
                   2);
        
        cv::imshow("摄像头画面", frame);
        
        int key = cv::waitKey(1) & 0xFF;
    
        if (key == 'q' || key == 27) {
            break;
        }
        
        cv::Mat rgba;

        cv::cvtColor(frame,rgba, cv::COLOR_BGR2BGRA);
        pushVideoDataForPushServiceWithRGBAData(rgba.data,width,height);


        // cv::cvtColor(frame,rgba,cv::COLOR_BGR2RGBA);
        // pushVideoDataForPushServiceWithBGRAData(rgba.data,width,height);

        // cv::cvtColor(frame,rgba,cv::COLOR_BGR2YUV_I420);
        // pushVideoDataForPushServiceWithYUVData(rgba.data,width,height);
    }
    
    releasePushService();

    cap.release();
    cv::destroyAllWindows();
    
    std::cout << "程序已退出" << std::endl;
    
    return 0;
}